package hgx.spring.cloud.lession.zk;/**
 * Created by liujun on 2018/6/17.
 */

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * @author hugx
 * @create 2018-06-17 12:16
 */
public class MyZkClient implements  Watcher,Runnable,MyDataMointor.MyDataListener{
    private ZooKeeper zk;
    private String hostPort="192.168.1.233:2181";
    private String node="/aaa";
    private boolean stopThread;
    private MyDataMointor myDataMointor;

    public MyZkClient() {
        try {
            zk=new ZooKeeper(hostPort,3000,this);
            myDataMointor=new MyDataMointor(zk,node,this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        synchronized (this){
            try {
                while (!stopThread) {
                    wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println("----- process() of MyZkClient");
        myDataMointor.process(event);

    }

    @Override
    public void close() {
        this.stopThread=true;
        synchronized (this){
            notifyAll();
        }
    }

    public static void main(String[] args) {
        System.out.println("========= 1 =======================");
        new Thread(new MyZkClient()).start();

        System.out.println("========= 2 =======================");
    }
}
